package com.yunhe.inspect.controller;

import com.yunhe.common.filter.ServerContextHolder;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.inspect.model.ExportHistoryVO;
import com.yunhe.inspect.model.HistoryQueryCondition;
import com.yunhe.inspect.service.ExportHistoryService;
import com.yunhe.inspect.service.HelpService;
import com.yunhe.inspect.service.HistoryService;
import com.yunhe.inspect.util.DownloadUtil;
import com.yunhe.inspect.util.MyHeaderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * 导出文件接口
 */
@Api(value = "history", description = "历史巡检管理接口", tags = {"Model_History"})
@RestController
@RequestMapping(value = "/history")
public class HistoryController {
    @Autowired
    private ExportHistoryService exportHistoryService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private MyHeaderUtil myHeaderUtil;
    @Autowired
    private HelpService helpService;

    @ApiOperation("历史巡检列表查询")
    @PostMapping("/findList")
    public ResponseEntity<ResultObject> findList(@RequestBody HistoryQueryCondition queryVO){
        queryVO.setStationId( ServerContextHolder.getContext().getProjectCode() );
        List<Long> orgIds = helpService.getOrgIds(queryVO.getOrgId(), Long.valueOf(queryVO.getStationId()), myHeaderUtil.getHeaderUser() );
        queryVO.setOrgIds(orgIds);
        PageResult pageResult = historyService.getListPage(queryVO);
        return new ResponseEntity<>(new ResultObject(pageResult),HttpStatus.OK);
    }

    @ApiOperation("查看历史巡检单详情")
    @GetMapping("/detail/{id}")
    public ResponseEntity<ResultObject> detail(@PathVariable("id") Long id){
        ExportHistoryVO vo = historyService.detail(id);
        return new ResponseEntity<>(new ResultObject(vo),HttpStatus.OK);
    }

    @ApiOperation("删除历史记录（单个与批量删除）")
    @DeleteMapping("/delete/{ids}")
    public ResponseEntity<ResultObject> delete(@PathVariable("ids") String ids){
        historyService.delete(ids, myHeaderUtil.getHeaderUser());
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }

    @ApiOperation("导出excel")
    @GetMapping("/export/{ids}")
    public void export( HttpServletResponse response, @PathVariable("ids") String ids) throws IOException {
        String filePath = exportHistoryService.export(ids);
        if(Objects.nonNull(filePath)){
            DownloadUtil.downloadExcel(response, filePath);
        }
    }

}
